package com.shiyun.marker.generator.main;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.StrUtil;
import com.shiyun.marker.generator.JarGenerator;
import com.shiyun.marker.generator.ScriptGenerator;
import com.shiyun.marker.generator.file.DynamicFileGenerator;
import com.shiyun.marker.meta.Meta;
import com.shiyun.marker.meta.MetaManager;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.IOException;

/**
 *
 */



public class MainGenerator {
    private void generatorCode(Meta meta, String outputPath) {

    }
    public static void main(String[] args) throws IOException, TemplateException, InterruptedException {
        Meta meta = MetaManager.getMetaObject();
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + File.separator + "generated" + File.separator + meta.getName();
        //判断目录是否存在
        if(!FileUtil.exist(outputPath)) {
            FileUtil.mkdir(outputPath);
        }
        //获取原始模板文件复制到生成器项目中
        String sourceRootPath = meta.getFileConfig().getSourceRootPath();
        String sourceCopyDestPath = outputPath + File.separator + meta.getFileConfig().getInputRootPath();
        FileUtil.copy(sourceRootPath,sourceCopyDestPath,false);

        //读取resources目录
        ClassPathResource classPathResource = new ClassPathResource("");
        String inputResourcePath = classPathResource.getAbsolutePath();

        //java包基础路径 com.shiyun
        String outputBasePackage = meta.getBasePackage();
        String outputBasePackagePath = StrUtil.join("/",StrUtil.split(outputBasePackage,'.'));
        String outputBaseJavaPackagePath = outputPath + File.separator + "src/main/java/" + outputBasePackagePath;

        String inputFilePath;
        String outputFilePath;

        //model.DataModel  生成DataModel文件
        inputFilePath = inputResourcePath + "templates/java/model/DataModel.java.ftl";
        outputFilePath = outputBaseJavaPackagePath+ File.separator + "/model/DataModel.java";
        DynamicFileGenerator.doGenerator(inputFilePath,outputFilePath,meta);

        //生成命令行文件
        inputFilePath = inputResourcePath + File.separator + "templates/cli/command/ConfigCommand.java.ftl";
        outputFilePath = outputBaseJavaPackagePath + File.separator + "/cli/command/ConfigCommand.java";
        DynamicFileGenerator.doGenerator(inputFilePath,outputFilePath,meta);

        inputFilePath = inputResourcePath + File.separator + "templates/cli/command/GeneratorCommand.java.ftl";
        outputFilePath = outputBaseJavaPackagePath + File.separator + "/cli/command/GeneratorCommand.java";
        DynamicFileGenerator.doGenerator(inputFilePath,outputFilePath,meta);

        inputFilePath = inputResourcePath + File.separator + "templates/cli/command/ListCommand.java.ftl";
        outputFilePath = outputBaseJavaPackagePath + File.separator + "/cli/command/ListCommand.java";
        DynamicFileGenerator.doGenerator(inputFilePath,outputFilePath,meta);


        //生成生成器文件
        inputFilePath = inputResourcePath + File.separator + "templates/java/generator/DynamicGenerator.java.ftl";
        outputFilePath = outputBaseJavaPackagePath + File.separator + "/generator/DynamicGenerator.java";
        DynamicFileGenerator.doGenerator(inputFilePath,outputFilePath,meta);

        inputFilePath = inputResourcePath + File.separator + "templates/java/generator/MainGenerator.java.ftl";
        outputFilePath = outputBaseJavaPackagePath + File.separator + "/generator/MainGenerator.java";
        DynamicFileGenerator.doGenerator(inputFilePath,outputFilePath,meta);

        inputFilePath = inputResourcePath + File.separator + "templates/java/generator/StaticGenerator.java.ftl";
        outputFilePath = outputBaseJavaPackagePath + File.separator + "/generator/StaticGenerator.java";
        DynamicFileGenerator.doGenerator(inputFilePath,outputFilePath,meta);

        //生成pom.xml，
        inputFilePath = inputResourcePath + File.separator + "templates/pom.xml.ftl";
        outputFilePath = outputPath + File.separator + "pom.xml";
        DynamicFileGenerator.doGenerator(inputFilePath,outputFilePath,meta);

        //构建jar包
        JarGenerator.doGenerator(outputPath);

        //封装脚本
        String shellOutputFilePath = outputPath + File.separator + "generator";
        String jarName = String.format("%s-%s-jar-with-dependencies.jar",meta.getName(),meta.getVersion());
        String jarPath = "target/"+jarName;
        ScriptGenerator.doGenerate(shellOutputFilePath,jarPath);
    }
}
